package org.rpgmapper.view;

import android.app.Activity;
import org.rpgmapper.R;
import org.rpgmapper.bo.Character;
import org.rpgmapper.bo.Stat;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.TextView;
import java.util.concurrent.Callable;
import org.rpgmapper.Main;
import org.rpgmapper.skirmish.SkirmishManager;

public class StatsView extends FrameLayout {

    private Character character;

    public StatsView(Context context) {
        super(context);
        init();
    }

    public StatsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public StatsView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.stats, this);

        GridView stats = (GridView) findViewById(R.id.stats);
        stats.setAdapter(new StatsAdapter());
        stats.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    return true;
                }
                return false;
            }
        });
         findViewById(R.id.remove).setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                SkirmishManager.getInstance().removeCharacter(character);                 
            }
        });
    }

    public void setCharacter(Character character) {
        this.character = character;
        this.character.refreshRuleSet();
        ((TextView) findViewById(R.id.summary)).setText(character.toString());
    }

    public void setOKClickListener(OnClickListener okClickListener) {
        findViewById(R.id.ok).setOnClickListener(okClickListener);
    }

    private class StatsAdapter extends BaseAdapter {

        private final Stat[] stats = new Stat[]{Stat.Level, Stat.Str, Stat.Dex, Stat.Con, Stat.Int, Stat.Wis, Stat.Cha,
            Stat.Ref, Stat.For, Stat.Wil, Stat.MovePoints, Stat.BBA, Stat.AC, Stat.HitPoints, Stat.MaxHitPoints};

        public int getCount() {
            return stats.length;
        }

        public Object getItem(int position) {
            return stats[position];
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            StatView ll = (StatView) convertView;
            if (ll == null) {
                ll = new StatView(getContext(), stats[position], onPickerValueChangeListener);
                ll.setValue(character.getBase(stats[position]));
            }
            return ll;
        }
    }
    private OnPickerValueChangeListener onPickerValueChangeListener = new OnPickerValueChangeListener() {

        public void onValueChange(final PickerView picker, int oldValue, final int newValue) {
            Main.getJmeApp().enqueue(new Callable<Void>() {

                public Void call() throws Exception {
                    character.setBase((Stat) picker.getTag(), newValue);       
                    character.refreshRuleSet();
                    ((Activity)getContext()).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ((TextView) findViewById(R.id.summary)).setText(character.toString());                            
                        }
                    });

                    return null;
                }
            });
            
        }
    };
}
